<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Services_Rule
{
	public static function add($rule)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$ruleDao = new Modules_Core_Models_Mysql_Rule();
		$ruleDao->setConnection($conn);
		/**
		 * Check Exist
		 */
		$exist = $ruleDao->checkExist($rule);
		if ($exist != null) {
			$rule['rule_id'] = $exist['rule_id'];
			return $ruleDao->update($rule);
		} else {
			return $ruleDao->add($rule);
		}
	}
	
	public static function getRules($objectType, $objectId)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$ruleDao = new Modules_Core_Models_Mysql_Rule();
		$ruleDao->setConnection($conn);
		return $ruleDao->getRules($objectType, $objectId);
	}
	
	public static function getAclRules()
	{
		$conn		= Gio_Db_Connection::getConnection();
		$ruleDao = new Modules_Core_Models_Mysql_Rule();
		$ruleDao->setConnection($conn);
		return $ruleDao->getAclRules();
	}
	
	public static function isAllowed($action, $controller = null, $module = null)
	{
		$phpSessionId   = $request->getPost('PHPSESSID');
		$session = Gio_Core_Session::getSessionById($phpSessionId);
		
		$json = new Services_JSON();
		$user = (null == $session || null == $session['data']) 
										? null : $json->decode($session['data']);
		
		if (null == $user) {
			return false;
		}
		
	}
	
	public static function reset($objectType, $objectId, $module)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$ruleDao = new Modules_Core_Models_Mysql_Rule();
		$ruleDao->setConnection($conn);
		return $ruleDao->reset($objectType, $objectId, $module);
	}
	
	public function deleteByRole($roleId)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$ruleDao = new Modules_Core_Models_Mysql_Rule();
		$ruleDao->setConnection($conn);
		return $ruleDao->deleteByRole($roleId);
	}
}